package com.studykun.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.Response;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.Widget;
import com.studykun.client.model.Syllabus;
import com.studykun.client.model.World;
import com.studykun.client.ui.CategoryChart;
import com.studykun.client.ui.ChatBox;
import com.studykun.client.util.ChatRoom;
import com.studykun.client.util.Presence;
import com.studykun.client.util.Student;
import com.studykun.client.util.SystemAgent;
import com.studykun.client.util.ValueChangeBehaviour;

import java.util.Arrays;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Studykun implements EntryPoint {
  /**
   * Create a remote service proxy to talk to the server-side Greeting service.
   */
  private final GreetingServiceAsync greetingService = GWT
      .create(GreetingService.class);
  
  private static final String JSON_URL = GWT.getHostPageBaseURL() + "/kanji.js";
  
  private final Panel clientPanel_ = RootPanel.get("mainClientPanel");
  
  private final DockPanel dockPanel_ = new DockPanel();
  
  private final CategoryChart progressChart_ = new CategoryChart();
  private final SimplePanel progressChartClient_ = new SimplePanel();

  private final TextArea chatInput_ =
    ValueChangeBehaviour.addTo(new TextArea()).getElement();
  private final SimplePanel chatInputWrapper_ = new SimplePanel();
  private final SimplePanel chatInputClient_ = new SimplePanel();
  
  private final ChatBox chatBox_ = new ChatBox();
  private final SimplePanel chatBoxClient_ = new SimplePanel();
  
  private World world_ = new World();
  
  private final SystemAgent systemAgent_ = new SystemAgent(world_);
  private final Student student_ = new Student();
  private final ChatRoom chatRoom_ =
    new ChatRoom(chatBox_, systemAgent_, world_, progressChart_);
  
  /**
   * This is the entry point method.
   */
  public void onModuleLoad() {
    world_.addRoom(chatRoom_);
    clientPanel_.add(dockPanel_);

    dockPanel_.add(progressChartClient_, DockPanel.NORTH);
    progressChartClient_.add(progressChart_);
    progressChartClient_.getElement().setClassName("progressChartClient");
    
    dockPanel_.add(chatBoxClient_, DockPanel.CENTER);
    chatBoxClient_.add(chatBox_);
    chatBoxClient_.getElement().setClassName("chatBoxClient");
    
    dockPanel_.add(chatInputClient_, DockPanel.SOUTH);
    chatInputClient_.add(chatInputWrapper_);
    chatInputWrapper_.add(chatInput_);
    chatInputClient_.getElement().setClassName("chatInputClient");
    
    chatRoom_.admit(systemAgent_);
    final Presence studentPresence = chatRoom_.admit(student_);
    
    final Widget category0 = new SimplePanel();
    final Widget category1 = new SimplePanel();
    final Widget category2 = new SimplePanel();
    final Widget category3 = new SimplePanel();
    final Widget category4 = new SimplePanel();
    category0.getElement().setClassName("red");
    category1.getElement().setClassName("orange");
    category2.getElement().setClassName("yellow");
    category3.getElement().setClassName("green");
    category4.getElement().setClassName("blue");
    progressChart_.setCategoryQuantities(Arrays.asList(1, 2, 3, 4, 5));
    progressChart_.setCategoryWidgets(Arrays.asList(
        category0, category1, category2, category3, category4));
    
    studentPresence.bindInput(chatInput_);
    
    systemAgent_.sayIn(chatRoom_, "Downloading syllabus ...");
    
    Syllabus.dowloadFrom(JSON_URL, new Syllabus.DownloadCallback() {
      public void onDownload(final Syllabus syllabus) {
        world_.synchronise(syllabus);
        systemAgent_.sayIn(chatRoom_, "Downloaded " + syllabus.decks().size() + " chapters.");
        systemAgent_.sayIn(chatRoom_, "For list of commands, type /help.");
        chatRoom_.admit(world_.teachers().get(0));
        chatInput_.addValueChangeHandler(new ValueChangeHandler<String>() {
          public void onValueChange(ValueChangeEvent<String> event) {
            //systemAgent_.sayIn(chatRoom_, chatInput_.getText());
          }
        });
      }

      public void onError(Request request, Response response, Throwable throwable) {
        systemAgent_.sayIn(chatRoom_, "Failed to load syllabus: " + throwable.getMessage());
        if (throwable != null) {
          systemAgent_.sayIn(chatRoom_, throwable.getMessage());
          for (final StackTraceElement stackTraceElement : throwable.getStackTrace()) {
            systemAgent_.sayIn(chatRoom_, stackTraceElement.toString());
          }
        }
      }
    });
  }

  public Syllabus getSyllabus() {
    return world_.getSyllabus();
  }
}
